export interface LogData{
    source?:string;
    message?:string;
    stack?:string;
    extended?:any;
}


export const mpjLog =  (function () {
    function objectTransToString(obj) {
        if (obj instanceof Object) {
            return JSON.stringify(obj);
        }
        else {
            return obj;
        }
    }
    function getArgsArray(args) {
        var argsArray = [];
        for (var i = 0; i < args.length; i++) {
            var m = objectTransToString(args[i]);
            argsArray.push(m);
        }
        return argsArray;
    }
    function logger(type, args:LogData) {
        switch (type) {
            case "log":
                console.log(args);
                break;
            case "error":
                console.error(args); //console.error吧js搞死了
                break;
            case "info":
                console.info(args);
                break;
            case "debug":
                console.debug(<any>args);
                break;
            case "warn":
                console.warn(args);
                break;
            default: throw "mpjlog error can not find type!";
        }

        var argsArray = getArgsArray(args);

        if ((<any>window).mconsole) {
            switch (type) {
                case "log":
                    (<any>window).mconsole.log(argsArray.join(","));
                    break;
                case "info":
                    (<any>window).mconsole.info(argsArray.join(","));
                    break;
                case "warn":
                    (<any>window).mconsole.warn(argsArray.join(","));
                    break;
                case "debug":
                    (<any>window).mconsole.debug(argsArray.join(","));
                    break;
                case "error":
                    (<any>window).mconsole.error(argsArray.join(","));
                    break;
                default: throw "mpjlog error can not find type!";
            }
        }
    }

    function log(logData:LogData) {
        logger("log", logData);
    }

    function error(logData:LogData) {
        logger("error", logData);
    }

    function info(logData:LogData) {
        logger("info", logData);
    }

    function debug(logData:LogData) {
        logger("debug", logData);
    }

    function warn(logData:LogData) {
        logger("warn", logData);
    }

    return {
        log: log,
        error: error,
        info: info,
        debug: debug,
        warn: warn,
    };
})()